package com.aat.dxfy.prop;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropDyload {
	private static Properties prop = new Properties();
	private static Long lastModified = 0l;
	private static String filepath = Thread.currentThread().getContextClassLoader().getResource("").getPath();

	private static void init() {
		try {
			FileInputStream fis = new FileInputStream(filepath + "test.properties");
			prop.load(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 判断是否被修改过
	public static boolean isPropertiesModified() {
		boolean returnValue = false;
		File file = new File(filepath + "test.properties");
		System.out.println(file.lastModified()+"  "+lastModified);
		if (file.lastModified() > lastModified) {
			lastModified = file.lastModified();
			returnValue = true;
		}
		return returnValue;
	}

	public static String getVal(String key) {
		if (prop == null || isPropertiesModified()) {
			init();
		}
		return prop.getProperty(key);
	}

	public static void main(String[] args) {

		try {
			while (true) {
				String key = getVal("key");
				System.out.println(key);
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
